import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import org.jsoup.Connection;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class Bullshit {

	/**
	 * @param user				Who u r
	 * @param psw				Who's ur gf
	 * @param thisWeekTarget	Eg: I wanna a gf
	 * @param everyDayThought	Er, every day, who kiss ur asshole
	 * @param thisWeekThought	What the fuck r u thinking for this week
	 * @param teamNO			Who r u belong to. Default value: 20(其他)
	 */
	@SuppressWarnings("deprecation")
	public String doit(String user, String psw, String thisWeekTarget, String everyDayThought, String thisWeekThought, String teamNO) {
		
		if (teamNO == null || "".equals(teamNO)) {
			teamNO = "20";
		}
		
		int timeout = 10000;
		
		try {
			// 请求首页
			Connection conn = Jsoup.connect("http://report.sarnath.cn")
					.timeout(timeout)
					.followRedirects(false)
					.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2")
					.referrer("http://report.sarnath.cn");
			Document doc = conn.get();
			
			String sessionId = conn.response().cookie("ASP.NET_SessionId");
			
			String url302 = URLDecoder.decode("http://report.sarnath.cn" + doc.getElementsByTag("a").get(0).attr("href"));
			
			conn = Jsoup.connect(url302)
					.timeout(timeout)
					.followRedirects(false)
					.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2")
					//.referrer("http://report.sarnath.cn")
					.cookie("ASP.NET_SessionId", sessionId)
					.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("Accept-Encoding", "gzip, deflate")
					.header("Connection", "keep-alive")
					.header("Host", "report.sarnath.cn")
					.header("Accept-Language", "zh-cn,zh;q=0.5")
					;
			doc = conn.get();
			
			Response resp = conn.response();
			String viewState = doc.select("#__VIEWSTATE").val();
			String viewStateEncrypted = doc.select("#__EVENTVALIDATION").val();
			 
			doc = Jsoup.connect(resp.url().toString())
				.timeout(timeout)
				.followRedirects(false)
				.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2")
			    .referrer(resp.url().toString())
			    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
				.header("Accept-Encoding", "gzip, deflate")
				.header("Connection", "keep-alive")
				.header("Host", "report.sarnath.cn")
				.header("Accept-Language", "zh-cn,zh;q=0.5")
			    .cookie("ASP.NET_SessionId", sessionId)
			    .data("__EVENTARGUMENT", "")
				.data("__EVENTTARGET", "")
				.data("__EVENTVALIDATION", viewStateEncrypted)
				.data("__LASTFOCUS", "")
			    .data("__VIEWSTATE", viewState)
			    .data("btnLogin", "登录")
			    .data("txtPassword", psw)
			    .data("txtUser", user)
			    .post();
			
			url302 = URLDecoder.decode("http://report.sarnath.cn" + doc.getElementsByTag("a").get(0).attr("href"));
			
			// 登入主界面
			conn = Jsoup.connect(url302)
					.timeout(timeout)
					.followRedirects(false)
					.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2")
				    .referrer(resp.url().toString())
				    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("Accept-Encoding", "gzip, deflate")
					.header("Connection", "keep-alive")
					.header("Host", "report.sarnath.cn")
					.header("Accept-Language", "zh-cn,zh;q=0.5")
				    .cookie("ASP.NET_SessionId", sessionId)
				    ;
			doc = conn.post();
			
			resp = conn.response();
			viewState = doc.select("#__VIEWSTATE").val();
			viewStateEncrypted = doc.select("#__EVENTVALIDATION").val();
			// 新建周报
			conn = Jsoup.connect(resp.url().toString())
					.timeout(timeout)
					.followRedirects(false)
					.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2")
				    .referrer(resp.url().toString())
				    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("Accept-Encoding", "gzip, deflate")
					.header("Connection", "keep-alive")
					.header("Host", "report.sarnath.cn")
					.header("Accept-Language", "zh-cn,zh;q=0.5")
				    .cookie("ASP.NET_SessionId", sessionId)
				    .data("__EVENTARGUMENT", "")
					.data("__EVENTTARGET", "ctl00$ContentPlaceHolder1$btnReport")
					.data("__EVENTVALIDATION", viewStateEncrypted)
					.data("__LASTFOCUS", "")
					.data("__VIEWSTATEENCRYPTED", "")
					.data("ctl00$ContentPlaceHolder1$hdnSelectCondition", "")
					.data("ctl00$ContentPlaceHolder1$hdnSelectFlag", "0")
					.data("ctl00$ContentPlaceHolder1$hdnTxtReportNameStart", "")
					.data("ctl00$ContentPlaceHolder1$hdnTxtReportNameEnd", "")
					.data("ctl00$ContentPlaceHolder1$ddlState", "")
					.data("ctl00$ContentPlaceHolder1$ddlPage", "1")
				    .data("__VIEWSTATE", viewState)
				    ;
			doc = conn.post();
			
			url302 = URLDecoder.decode("http://report.sarnath.cn" + doc.getElementsByTag("a").get(0).attr("href"));
			
			resp = conn.response();
			conn = Jsoup.connect(url302)
					.timeout(timeout)
					.followRedirects(false)
					.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2")
				    .referrer(resp.url().toString())
				    .header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
					.header("Accept-Encoding", "gzip, deflate")
					.header("Connection", "keep-alive")
					.header("Host", "report.sarnath.cn")
					.header("Accept-Language", "zh-cn,zh;q=0.5")
				    .cookie("ASP.NET_SessionId", sessionId)
				    ;
			doc = conn.get();
			
			Calendar cal = Calendar.getInstance(Locale.CHINA);
			cal.setFirstDayOfWeek(Calendar.MONDAY);
			
			String weekOfYear = String.valueOf(cal.get(Calendar.WEEK_OF_YEAR) - 1);
			
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			String monday = sdf.format(cal.getTime());
			cal.set(Calendar.DAY_OF_WEEK, Calendar.TUESDAY);
			String tuesday = sdf.format(cal.getTime());
			cal.set(Calendar.DAY_OF_WEEK, Calendar.WEDNESDAY);
			String wednesday = sdf.format(cal.getTime());
			cal.set(Calendar.DAY_OF_WEEK, Calendar.THURSDAY);
			String thursday = sdf.format(cal.getTime());
			cal.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
			String friday = sdf.format(cal.getTime());
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
			String saturday = sdf.format(cal.getTime());
			cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
			String sunday = sdf.format(cal.getTime());
			
			sdf = new SimpleDateFormat("yyyy/MM/dd");
			String endDay = sdf.format(cal.getTime());
			cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
			String startDay = sdf.format(cal.getTime());
			
			// 提交
			resp = conn.response();
			viewState = doc.select("#__VIEWSTATE").val();
			conn = Jsoup.connect(resp.url().toString())
					.timeout(timeout)
					.followRedirects(false)
					.userAgent("Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2")
				    .referrer(resp.url().toString())
				    .header("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*")
					.header("Accept-Encoding", "gzip, deflate")
					.header("Content-Type", "application/x-www-form-urlencoded")
					.header("Connection", "keep-alive")
					.header("Cache-Control", "no-cache")
					.header("Host", "report.sarnath.cn")
					.header("Accept-Language", "zh-cn")
				    .cookie("ASP.NET_SessionId", sessionId)
				    .data("__EVENTTARGET", "")
				    .data("__EVENTARGUMENT", "")
				    .data("__VIEWSTATE", viewState)
				    .data("__SCROLLPOSITIONX", "0")
				    .data("__SCROLLPOSITIONY", "411")
					.data("ctl00$ContentPlaceHolder1$hdnTeam", teamNO)
					.data("ctl00$ContentPlaceHolder1$hdnStartDay", startDay)
					.data("ctl00$ContentPlaceHolder1$hdnEndDay", endDay)
					.data("ctl00$ContentPlaceHolder1$hdnWeekID", weekOfYear)
					.data("ctl00$ContentPlaceHolder1$hdnWeekOne", everyDayThought)
					.data("ctl00$ContentPlaceHolder1$hdnWeekTwo", everyDayThought)
					.data("ctl00$ContentPlaceHolder1$hdnWeekThree", everyDayThought)
					.data("ctl00$ContentPlaceHolder1$hdnWeekFour", everyDayThought)
					.data("ctl00$ContentPlaceHolder1$hdnWeekFive", everyDayThought)
					.data("ctl00$ContentPlaceHolder1$hdnWeekSix", "")
					.data("ctl00$ContentPlaceHolder1$hdnWeekSeven", "")
					.data("ctl00$ContentPlaceHolder1$hdnWeekTarget", thisWeekTarget)
					.data("ctl00$ContentPlaceHolder1$hdnWeekLeave", "")
					.data("ctl00$ContentPlaceHolder1$hdnWeekThink", thisWeekThought)
					.data("ctl00$ContentPlaceHolder1$hdnBackFlg", "")
					.data("ctl00$ContentPlaceHolder1$hdnCount", "0")
					.data("ctl00$ContentPlaceHolder1$hdnTxt0", monday)
					.data("ctl00$ContentPlaceHolder1$hdnTxt1", tuesday)
					.data("ctl00$ContentPlaceHolder1$hdnTxt2", wednesday)
					.data("ctl00$ContentPlaceHolder1$hdnTxt3", thursday)
					.data("ctl00$ContentPlaceHolder1$hdnTxt4", friday)
					.data("ctl00$ContentPlaceHolder1$hdnTxt5", saturday)
					.data("ctl00$ContentPlaceHolder1$hdnTxt6", sunday)
					.data("ctl00$ContentPlaceHolder1$hdnWeekNum", "4")
					.data("ctl00$ContentPlaceHolder1$drpTeam", teamNO)
					.data("ctl00$ContentPlaceHolder1$btnCommit", URLDecoder.decode("+%E6%8F%90+%E4%BA%A4", "utf-8"))
				    ;
			doc = conn.post();
			
			return user + " 亲，恭喜你，成功了，买彩票去吧，你硬了，好吧，我是故意写错的";
		} catch (IOException e) {
			e.printStackTrace();
			return user + " 亲，您的网速太特么不给力了，多试几次，总会好的";
		}
	}
	
	public static void main(String[] args) {
		
		Bullshit bs = new Bullshit();
		
		File file = new File("info.txt");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			String info = null;
			String[] arr;
			while (null != (info = br.readLine())) {
				arr = info.split(",");
				String rs = bs.doit(arr[0], arr[1], arr[2], arr[3], arr[4], (arr.length > 5 ? arr[5] : null));
				System.out.println(rs);
			}
		} catch (FileNotFoundException e) {
			System.out.println("亲，没找到info.txt文件");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("坟蛋，你的info.txt格式可能不对！！！");
		}
		
		System.out.println("猛戳你的G点，退出。。。");
		try {
			System.in.read();
		} catch (IOException e) {
		}
	}
}
